home *** CD-ROM | disk | FTP | other *** search
- #include <AppleEvents.h>
- #include "AppEventDefs.h"
- #include "AppEvents.h"
- #include "MyHandlers.h"
- #include "TestBed.h"
- #include "Gripe.h"
-
- static StringPtr gServiceName;
-
- // Apple Event Handlers
- static AEEventHandlerUPP gOAPPHandlerUPP = (AEEventHandlerUPP)NULL;
- static AEEventHandlerUPP gODOCHandlerUPP = (AEEventHandlerUPP)NULL;
- static AEEventHandlerUPP gPDOCHandlerUPP = (AEEventHandlerUPP)NULL;
- static AEEventHandlerUPP gQUITHandlerUPP = (AEEventHandlerUPP)NULL;
- static AEEventHandlerUPP gAnswerHandlerUPP = (AEEventHandlerUPP)NULL;
-
- OSErr InitReqHandlers( void )
- {
- OSErr err;
-
- gOAPPHandlerUPP = NewAEEventHandlerProc( MyOAPPHandler );
- if ( !gOAPPHandlerUPP )
- return memFullErr;
-
- if ( err = AEInstallEventHandler( kCoreEventClass,
- kAEOpenApplication,
- (AEEventHandlerUPP)gOAPPHandlerUPP,
- 0,
- false ) ){
- Gripe( "\poapp install failed" );
- return err;
- }
-
- gODOCHandlerUPP = NewAEEventHandlerProc( MyODocHandler );
- if ( !gODOCHandlerUPP )
- return memFullErr;
-
- if ( err = AEInstallEventHandler( kCoreEventClass,
- kAEOpenDocuments,
- (AEEventHandlerUPP)gODOCHandlerUPP,
- 0,
- false ) ){
- Gripe( "\podoc install failed" );
- return err;
- }
-
- gPDOCHandlerUPP = NewAEEventHandlerProc( MyPDocHandler );
- if ( !gPDOCHandlerUPP )
- return memFullErr;
-
- if ( err = AEInstallEventHandler( kCoreEventClass,
- kAEPrintDocuments,
- (AEEventHandlerUPP)gPDOCHandlerUPP,
- 0,
- false ) ){
- Gripe( "\ppdoc install failed" );
- return err;
- }
-
- gQUITHandlerUPP = NewAEEventHandlerProc( MyQuitHandler );
- if ( !gQUITHandlerUPP )
- return memFullErr;
-
- if ( err = AEInstallEventHandler( kCoreEventClass,
- kAEQuitApplication,
- (AEEventHandlerUPP)gQUITHandlerUPP,
- 0,
- false ) ){
- Gripe( "\pquit install failed" );
- return err;
- }
-
- gAnswerHandlerUPP = NewAEEventHandlerProc( MyAnswerHandler );
- if ( !gAnswerHandlerUPP )
- return memFullErr;
-
- if ( err = AEInstallEventHandler( kCoreEventClass,
- kAEAnswer,
- (AEEventHandlerUPP)gAnswerHandlerUPP,
- 0,
- false ) ){
- Gripe( "\pAnswer install failed" );
- return err;
- }
-
- return noErr;
- }
-
- OSErr TearDownReqHandlers( void )
- {
- #ifdef GENERATINGCFM
- if ( gOAPPHandlerUPP )
- DisposeRoutineDescriptor( gOAPPHandlerUPP );
-
- if ( gODOCHandlerUPP )
- DisposeRoutineDescriptor( gODOCHandlerUPP );
-
- if ( gPDOCHandlerUPP )
- DisposeRoutineDescriptor( gPDOCHandlerUPP );
-
- if ( gQUITHandlerUPP )
- DisposeRoutineDescriptor( gQUITHandlerUPP );
-
- if ( gAnswerHandlerUPP )
- DisposeRoutineDescriptor( gAnswerHandlerUPP );
-
- #endif
- return noErr;
- }
-
- /* This gets a connection using the PPC browser. From IM VI p. 6-58 */
-
- OSErr GetTargetAddress( StringPtr myPrompt,
- StringPtr myAppStr,
- PortInfoRec *myPortInfoPtr,
- AEAddressDesc *targetAddressPtr,
- StringPtr serviceName,
- TargetID *toTargetIDPtr )
- {
- OSErr err;
- PPCFilterUPP browseProc;
-
- gServiceName = serviceName;
-
- browseProc = NewPPCFilterProc( MyPPCBrowseProc );
- if ( !browseProc )
- return memFullErr;
-
- err = PPCBrowser( myPrompt,
- myAppStr,
- false,
- &( toTargetIDPtr->location ),
- myPortInfoPtr,
- (PPCFilterUPP)browseProc,
- (StringPtr)"\p" );
-
- #ifdef GENERATINGCFM
- DisposeRoutineDescriptor( browseProc );
- #endif
-
- if ( err )
- return err;
-
- BlockMove( &(myPortInfoPtr->name),
- &(toTargetIDPtr->name),
- sizeof(myPortInfoPtr->name) );
-
- err = AECreateDesc( typeTargetID,
- (Ptr)toTargetIDPtr,
- sizeof( *toTargetIDPtr ),
- targetAddressPtr );
-
- return err;
- }
-
- pascal Boolean MyPPCBrowseProc( LocationNamePtr theLoc, PortInfoPtr thePortInfo )
- {
- return true;
- #ifdef NEVER
- if ( thePortInfo->name.portKindSelector == ppcByString ){
- if ( PStrCmp( thePortInfo->name.u.portTypeStr, (StringPtr)"\pWORDSERVICES" ) )
- return true;
- else
- return false;
- }
- return false;
- #endif
- }
-
- Boolean PStrCmp( StringPtr foo, StringPtr bar )
- {
- short len;
-
- len = (short)( foo[ 0 ] );
-
- len += 1; /* include the length byte itself */
-
- while ( len-- )
- if ( *foo++ != *bar++ )
- return false;
-
- return true;
- }
-
- /* GetEventType is a utility routine to get the event class and ID out of an apple event */
-
- OSErr GetEventType( AppleEvent *theAppleEventPtr,
- AEEventClass *theClassPtr,
- AEEventID *theIDPtr )
- {
- OSErr err;
- Size realSize;
- DescType realType;
-
- /* Get the event class */
-
- err = AEGetAttributePtr( theAppleEventPtr,
- keyEventClassAttr,
- typeType,
- &realType,
- (Ptr)theClassPtr,
- (Size)sizeof( AEEventClass ),
- &realSize );
-
- if ( err )
- return err;
-
- /* Get the event ID */
-
- err = AEGetAttributePtr( theAppleEventPtr,
- keyEventIDAttr,
- typeType,
- &realType,
- (Ptr)theIDPtr,
- (Size)sizeof( AEEventID ),
- &realSize );
-
-
- return err;
- }
-
- OSErr GetEventID( AppleEvent *theAppleEventPtr,
- AEEventID *theIDPtr )
- {
- OSErr err;
- Size realSize;
- DescType realType;
-
- /* Get the event ID */
-
- err = AEGetAttributePtr( theAppleEventPtr,
- keyEventIDAttr,
- typeType,
- &realType,
- (Ptr)theIDPtr,
- (Size)sizeof( AEEventID ),
- &realSize );
-
-
- return err;
- }
-